/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applybook.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BizApplyBookMapper;
import com.nmf.dao.gen.bean.BizApplyBook;
import com.nmf.dao.gen.bean.BizApplyBookExample;
import com.nmf.site.biz.applybook.enums.BookStatusEnums;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.service.SysNameService;
import com.nmf.site.common.utils.IdGenerater;
import com.nmf.site.modules.sys.service.RoleService;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 申请书Service
 * @author bm
 * @version 2019-01-25
 */
@Service
@Transactional(readOnly = true)
public class BizApplyBookService{

	@Autowired
	BizApplyBookMapper bizApplyBookMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizApplyBook get(String no) {
		return bizApplyBookMapper.selectByPrimaryKey(no);
	}
	
	public List<BizApplyBook> findList(BizApplyBook bizApplyBook) {
		BizApplyBookExample bizApplyBookExample = new BizApplyBookExample();
		BizApplyBookExample.Criteria criteria = bizApplyBookExample.createCriteria();
		
		return bizApplyBookMapper.selectByExample(bizApplyBookExample);
	}
	
	public boolean isVal(String periodId) {
		BizApplyBookExample bizApplyBookExample = new BizApplyBookExample();
		BizApplyBookExample.Criteria criteria = bizApplyBookExample.createCriteria();
		String userId = UserUtils.getUser().getId();
		criteria.andUserIdEqualTo(userId);
		criteria.andPeriodIdEqualTo(periodId);
		List<BizApplyBook> blist =  bizApplyBookMapper.selectByExample(bizApplyBookExample);
		return blist.size()>0;
	}
	@Autowired
	RoleService roleService;
	public Page<BizApplyBook> findPage(Page<BizApplyBook> page, BizApplyBook bizApplyBook) {
		BizApplyBookExample bizApplyBookExample = new BizApplyBookExample();
		BizApplyBookExample.Criteria criteria = bizApplyBookExample.createCriteria();
		
		setCriteria(criteria,bizApplyBook);
		
		if(SysNameService.yttEnname.equals(roleService.queryRoleEnname(UserUtils.getUser().getId()))) {
			criteria.andOptIdEqualTo(UserUtils.getUser().getId());
		}
		List<BizApplyBook> dlist = bizApplyBookMapper.selectByExample(bizApplyBookExample);
		page.setList(dlist);
		page.setCount(bizApplyBookMapper.countByExample(bizApplyBookExample));
		
		return page;
	}
	
	private void setCriteria(BizApplyBookExample.Criteria criteria, BizApplyBook bizApplyBook) {
		if(StringUtils.isNotBlank(bizApplyBook.getCategoryId())) {
			criteria.andCategoryIdEqualTo(bizApplyBook.getCategoryId());
		}
		
		if(bizApplyBook.getApplyType()!=null) {
			criteria.andApplyTypeEqualTo(bizApplyBook.getApplyType());
		}
		
//		if(bizApplyBook.getBookStatus()!=null) {
//			criteria.andBookStatusEqualTo(bizApplyBook.getBookStatus());
//		}
	}
	public Page<BizApplyBook> queryByProvince(Page<BizApplyBook> page, BizApplyBook bizApplyBook) {
		BizApplyBookExample bizApplyBookExample = new BizApplyBookExample();
		BizApplyBookExample.Criteria criteria = bizApplyBookExample.createCriteria();
		
		setCriteria(criteria,bizApplyBook);
		if(StringUtils.isNotBlank(UserUtils.getUserAreaId())) {
			criteria.andProvinceIdEqualTo(UserUtils.getUserAreaId());
		}
		bizApplyBookExample.setPage(page);
		
		List<BizApplyBook> dlist = bizApplyBookMapper.selectByExample(bizApplyBookExample);
		page.setList(dlist);
		page.setCount(bizApplyBookMapper.countByExample(bizApplyBookExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BizApplyBook bizApplyBook) {
		if(StringUtils.isNotEmpty(bizApplyBook.getId())&&get(bizApplyBook.getId())!=null) {
			bizApplyBookMapper.updateByPrimaryKeySelective(bizApplyBook);
		}else {
			
			bizApplyBook.setUserId(UserUtils.getUser().getId());
			bizApplyBook.setUserName(UserUtils.getUser().getName());
			bizApplyBook.setProvinceId(UserUtils.getUser().getOffice().getArea().getId());
			bizApplyBook.setProvinceName(UserUtils.getUser().getOffice().getArea().getName());
			
			Date date = new Date();
			bizApplyBook.setCreateDate(date);
			bizApplyBook.setId(idGenerater.genClsNo("bizApplyBook"));
			bizApplyBook.setBookStatus(BookStatusEnums.INIT.value);
			bizApplyBook.setUpdateDate(date);
			bizApplyBookMapper.insertSelective(bizApplyBook);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizApplyBook bizApplyBook) {
		bizApplyBookMapper.deleteByPrimaryKey(bizApplyBook.getId());
	}
	
}